<?php
/*
 * @Description: 邮件发送处理
 * @Author: lifeng
 * @Date: 2019-01-21 15:00:48
 * @LastEditTime: 2019-01-21 16:53:42
 * @LastEditors: Please set LastEditors
 */

//获取当前文件所在的目录的上级目录
$_strfilePath = dirname(dirname(__FILE__));
//引入PHPMailer加载文件
require $_strfilePath.'/PHPMailer/PHPMailerAutoload.php';

/**
 * @description: 邮件发送函数
 * @param $host 邮件服务器host
 * @param $fromEmail 发送邮件的邮箱
 * @param $fromPassword 发送邮件的密码
 * @param $fromName  发送邮件邮箱的别名
 * @param $toEmial 接收邮件的邮箱
 * @param $toName 接收邮件邮箱的别名
 * @param $subject 邮件主题
 * @param $content 邮件正文内容
 * @return mixed
 */
function sendEmail($host,$fromEmail,$fromPassword,$fromName,$toEmial,$toName,$subject,$content){
    $mail = new PHPMailer(true);
    try {
        //Server settings
        $mail->isSMTP();                          // Set mailer to use SMTP
        $mail->Host = $host;                      // 邮件服务器地址
        $mail->SMTPAuth = true;                   // 邮件发送验证
        $mail->CharSet = 'UTF-8';                 //编码格式
        $mail->Username = $fromEmail;             //邮件服务器用户名（邮箱用户名）
        $mail->Password = $fromPassword;          //邮箱密码
    
        //Recipients
        $mail->setFrom($fromEmail, $fromName);     //设置发件人
        $mail->addAddress($toEmial, $toName);      //设置接收人
        $mail->addReplyTo($fromEmail, $fromName);  //设置接收的回复
    
        //Attachments
        // $mail->addAttachment('/var/tmp/file.tar.gz');         // 增加附件
        // $mail->addAttachment('/tmp/image.jpg', 'new.jpg');    // 附件设置别名
    
        //Content
        $mail->isHTML(true);                                  //设置识别html标签
        $mail->Subject = $subject;                            //邮件主题
        $mail->Body    = $content;                            //邮件正文内容
        // $mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
        return $mail->send();
    } catch (Exception $e) {
        return $e;
    }
}
/**
 * 数据库查询代码端
 */
$link = mysql_connect('localhost','root','root');
mysql_select_db('article_spider');
mysql_query('set names utf8');

while(true){
    $query = 'select task_id,user_email,status,create_time,update_time from task_list where status = 0 ORDER BY task_id ASC LIMIT 5';
    $result = mysql_query($query);
    $mailList = array();
    while($row = mysql_fetch_assoc($result)){
        $mailList[] = $row;
    }
    
    if(empty($mailList)){
        break;
    }else{
        foreach($mailList as $key=>$value){
            $subject = '这是php+mysql队列邮件发送的测试邮件'.time();
            $content = '这是测试邮件的正文内容'.time();
            $returnInfo = sendEmail('smtp.aliyun.com','happykala@aliyun.com','@lf*19910316','aliyun','18963970962@sina.cn','sina',$subject,$content);
            if($returnInfo){
                mysql_query("UPDATE task_list set STATUS = 1 WHERE task_id = ".$value['task_id']);
            }else{
               if($returnInfo instanceof phpmailerException){
                   $exceptionMessage = json_encode($returnInfo);
                   mysql_query("INSERT send_email_exception (task_id,exception_message) value (".$value['task_id'].",'".$exceptionMessage."')");
               } 
            }
            sleep(3);
        }
    }
}
